package com.qububu.emporium.goodsType.control;

import static com.qububu.utils.ParamConverter.assertNotEmpty;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qububu.emporium.goodsType.dto.BigTypeDto;
import com.qububu.emporium.goodsType.dto.LittleTypeDto;
import com.qububu.emporium.goodsType.service.GoodsTypeService;
import com.qububu.utils.ParamConverter;
import com.qububu.utils.SystemConstants;

@Controller
@RequestMapping("/gtype")
public class GoodsTypeControl {

	@Resource
	private GoodsTypeService goodsTypeService; 
	
	/**
	 * @author ZCW
	 * @功能 查出所有进入商城时需要的大的分类
	 * */
	@RequestMapping("/mainTypes")
	public String searchGoodsBigTypes(HttpServletRequest req) {
		
		List<BigTypeDto> bigTypeList = goodsTypeService.getGoodsBigTypes();
		
		// 将需要的数据存入内存
		req.setAttribute("mainTypes", bigTypeList);
		
//		return "goodsInfo";
		return "emporium";
	}
	
	/**
	 * @author ZCW
	 * @功能 获取需要显示的对应大分类下面的小分类
	 * @备注 应该放在show中
	 * */
	@RequestMapping("/ajaxSearchGoodsLittleTypes")
	@ResponseBody
	public Map<String, Object> ajaxSearchGoodsLittleTypes(HttpServletRequest req) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		String bigTypeId = assertNotEmpty(req.getParameter("typeP"));
		
		//String reBigTypeIdStr = SystemUtil.decrypt(bigTypeId);
		int reGoodsBigTypeId = ParamConverter.convertToInteger(bigTypeId);
		
		// 根据大的分类得出小的分类列表
		List<LittleTypeDto> littleTypeList  = goodsTypeService.getLittleTypeByBigTypeId(reGoodsBigTypeId);
		
		map.put("result", SystemConstants.AJAX_SUCCESS);
		map.put("typePid", bigTypeId);
		map.put("littleTypes", littleTypeList);
		
		return map;
	}
	
	/**
	 * @author ZCW
	 * @功能 获取所有大类型和小类型
	 * */
	@RequestMapping("/showGoods")
	public String searchGoodsBigTypesAndLittleTypes(HttpServletRequest req) {
		
		Map<String, List<?>> resultMap = null;
		
		List<BigTypeDto> bigTypeList = goodsTypeService.getGoodsBigTypes();
		
		// 查询出所有小类型
//		List<LittleTypeDto> littleTypeList  = goodsTypeService.getLittleTypeByBigTypeId(reGoodsBigTypeId);
		
		resultMap = new HashMap<String, List<?>>();
		
		resultMap.put("bigTypes", bigTypeList);
		
		req.setAttribute("types", resultMap);
		
		return "emporium";
	}
}
